package com.bwie.bwedu.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.presenter.impl.LoginPresenterImpl;
import com.bwie.bwedu.ui.ClearEditText;
import com.bwie.bwedu.view.BaseView;

import java.util.Random;

/**
 * Created by Administrator on 2016/7/21.
 */
public class LoginActivity extends BaseActivity implements BaseView {

    private ClearEditText username;
    private ClearEditText pwd;
    private Button login;
    private LoginPresenterImpl presenter;
    private ImageView img;
    private AnimatorSet set;
    private ImageView img2;
    private int[] imgs = {R.mipmap.main_back1, R.mipmap.main_back2, R.mipmap.main_back3, R.mipmap.main_back4};
    private Random random;
    private int last;
    private CheckBox checkbox;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private CheckBox ck;
    private boolean isChecked;

    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void findView() {
        username = (ClearEditText) findViewById(R.id.login_username);
        pwd = (ClearEditText) findViewById(R.id.login_pwd);
        login = (Button) findViewById(R.id.login_login);
        img = (ImageView) findViewById(R.id.login_back_img);
        img2 = (ImageView) findViewById(R.id.login_back_img2);
        checkbox = (CheckBox) findViewById(R.id.checkbox);
        ck = (CheckBox) findViewById(R.id.login_ck_remember);
        sharedPreferences = getSharedPreferences("rempwd", 1);
        String getName = sharedPreferences.getString("name", "");
        String getPwd = sharedPreferences.getString("pwd", "");
//        if (!TextUtils.isEmpty(getName) && !TextUtils.isEmpty(getPwd)) {
//            pwd.setText(getPwd);
//            username.setText(getName);
//            username.setSelection(getName.length());
//            ck.setChecked(true);
//        }

        if (sharedPreferences.getBoolean("isChecked",false)) {
            pwd.setText(getPwd);
            username.setText(getName);
            username.setSelection(getName.length());
            ck.setChecked(true);
        }else {
            ck.setChecked(false);
        }

    }

    @Override
    public void setData() {
        presenter = new LoginPresenterImpl(this);
        random = new Random();
        startAnim();

    }

    private void startAnim() {
        set = new AnimatorSet();

        //第一张图片显示
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(img, "alpha", 0, 0.5f).setDuration(2000);
        //第一张图片透明渐变、顺时针旋转36、缩放0.6
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(img, "alpha", 0.5f, 0).setDuration(2000);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(img, "rotation", 0, 36).setDuration(2000);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(img, "scaleX", 1, 0.6f).setDuration(2000);
        ObjectAnimator animator5 = ObjectAnimator.ofFloat(img, "scaleY", 1, 0.6f).setDuration(2000);

        //还原第一张
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("rotation", 36, 0);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 0.6f, 1);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 0.6f, 1);
        ObjectAnimator imgBack = ObjectAnimator.ofPropertyValuesHolder(img, holder1, holder2, holder3);

        //完成后图片改为第二张
        ObjectAnimator animator6 = ObjectAnimator.ofFloat(img2, "alpha", 0, 0.5f).setDuration(2000);

        //第二张图片透明渐变、逆时针旋转36、缩放0.6
        ObjectAnimator animator7 = ObjectAnimator.ofFloat(img2, "alpha", 0.5f, 0).setDuration(2000);
        ObjectAnimator animator8 = ObjectAnimator.ofFloat(img2, "scaleX", 1, 0.6f).setDuration(2000);
        ObjectAnimator animator9 = ObjectAnimator.ofFloat(img2, "scaleY", 1, 0.6f).setDuration(2000);
        ObjectAnimator animator10 = ObjectAnimator.ofFloat(img2, "rotation", 0, -36).setDuration(2000);

        //还原第二张
        PropertyValuesHolder holder4 = PropertyValuesHolder.ofFloat("rotation", -36, 0);
        PropertyValuesHolder holder5 = PropertyValuesHolder.ofFloat("scaleX", 0.6f, 1);
        PropertyValuesHolder holder6 = PropertyValuesHolder.ofFloat("scaleY", 0.6f, 1);
        ObjectAnimator img2Back = ObjectAnimator.ofPropertyValuesHolder(img2, holder4, holder5, holder6).setDuration(0);
        set.play(animator2)
                .with(animator3)
                .with(animator4)
                .with(animator5)
                .after(animator1);
        set.play(animator6).with(imgBack).after(animator2);
        set.play(animator7).with(animator8).with(animator9).with(animator10).after(animator6);
        set.play(img2Back).after(animator7);
        set.start();
        last = R.mipmap.test2;
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                int num1 = random.nextInt(imgs.length);
                int num2 = random.nextInt(imgs.length);
                while (num1 == last) {
                    num1 = random.nextInt(imgs.length);
                }
                while (num1 == num2) {
                    num2 = random.nextInt(imgs.length);
                }
                last = num2;
                img.setImageResource(imgs[num1]);
                img2.setImageResource(imgs[num2]);
                set.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }

    //对登录按钮设置监听
    @Override
    public void setListener() {
        login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                presenter.login(username.getText().toString(), pwd.getText().toString());
//                sharedPreferences=getSharedPreferences("rempwd",1);
//                editor = sharedPreferences.edit();
//                if(checkbox.isChecked()){
//                    editor.putString("name",username.getText().toString());
//                    editor.putString("pwd",pwd.getText().toString());
//                }
//                else{
//                    editor.clear();
//                }
//                editor.commit();
            }
        });
    }

    @Override
    public void onSuccess() {
        editor = sharedPreferences.edit();
        if (ck.isChecked()) {
            editor.putString("name", username.getText().toString());
            editor.putString("pwd", pwd.getText().toString());
            editor.putBoolean("isChecked",true);
        } else {
            editor.clear();
        }
        editor.commit();
        startActivity(new Intent(this, MainActivity.class));
        finish();
        set.cancel();
        overridePendingTransition(R.anim.rightenter, R.anim.leftexit);
    }

    @Override
    public void onFail(String info) {
        showToast("" + info);
    }
}
